package com.zhuanzhuan.hero.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.google.common.collect.Lists;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.dto.label.QueryBrandModelLabelDTO;
import com.zhuanzhuan.hero.user.entity.HeroLabelInfo;
import com.zhuanzhuan.hero.user.enums.LabelCodeEnum;
import com.zhuanzhuan.hero.user.enums.UserAuthTypeEnum;
import com.zhuanzhuan.hero.user.facade.label.ContentCategoryFacade;
import com.zhuanzhuan.hero.user.service.HeroLabelInfoService;
import com.zhuanzhuan.hero.user.vo.IdAndNameVO;
import com.zhuanzhuan.hero.user.vo.label.LevelLabelVO;
import com.zhuanzhuan.youpin.exceptions.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HeroLabelHandleService {

    private final static String LABEL_ADVANTAGE = "2-BRAND";
    private final static String LABEL_FINENESS = "2-D1GD";
    private final static String LABEL_FINENESS1 = "2-ZMV7";
    private final static String LABEL_CHANNEL = "1-19L5";
    private final static String LABEL_MAINCATE = "1-4849";
    private final static String LABEL_SERVICE = "1-A6H8";


    @Resource
    private HeroLabelInfoService labelInfoService;


    /**
     * 优势品牌
     */
    public List<IdAndNameVO> getAdvantageCateList() {
        //盘古9
        QueryBrandModelLabelDTO dto = new QueryBrandModelLabelDTO();
        dto.setLabelCode(LabelCodeEnum.CONTENT_CATE.getCode());
        dto.setLabelValue(NumConsts.MOBILE_CATEID);
        List<LevelLabelVO> levelLabelVOS = labelInfoService.queryContentCategoryLabels(dto);
        if (ObjectUtil.isEmpty(levelLabelVOS)) {
            return Lists.newArrayList();
        }
        return levelLabelVOS.stream().map(x -> new IdAndNameVO(String.valueOf(x.getLabelValue()), x.getLabelName())).collect(Collectors.toList());
//        return heroLabelInfos.stream().filter(x -> LABEL_ADVANTAGE.equals(x.getParentLabelCode()))
//                .sorted(Comparator.comparing(HeroLabelInfo::getShowOrder))
//                .map(x -> new IdAndNameVO(x.getLabelCode(), x.getLabelName()))
//                .collect(Collectors.toList());
    }

    /**
     * 主营成色
     */
    public List<IdAndNameVO> getFinenessList(List<HeroLabelInfo> heroLabelInfos) {

        return heroLabelInfos.stream().filter(x -> LABEL_FINENESS1.equals(x.getParentLabelCode()) || LABEL_FINENESS.equals(x.getParentLabelCode()))
                .sorted(Comparator.comparing(HeroLabelInfo::getShowOrder))
                .map(x -> new IdAndNameVO(x.getLabelCode(), x.getLabelName()))
                .collect(Collectors.toList());
    }

    /**
     * 渠道偏好
     */
    public List<IdAndNameVO> getChannelPreferenceList(List<HeroLabelInfo> heroLabelInfos) {
        List<String> collect = heroLabelInfos.stream().filter(x -> LABEL_CHANNEL.equals(x.getParentLabelCode()))
                .sorted(Comparator.comparing(HeroLabelInfo::getShowOrder))
                .map(x -> x.getLabelCode())
                .collect(Collectors.toList());

        return heroLabelInfos.stream().filter(x -> collect.contains(x.getParentLabelCode()))
                .sorted(Comparator.comparing(HeroLabelInfo::getShowOrder))
                .map(x -> new IdAndNameVO(x.getLabelCode(), x.getLabelName()))
                .collect(Collectors.toList());
    }

    /**
     * 主营品类
     */
    public List<IdAndNameVO> getMainCateList(List<HeroLabelInfo> heroLabelInfos) {
        return heroLabelInfos.stream().filter(x -> LABEL_MAINCATE.equals(x.getParentLabelCode())).flatMap(l -> {
                    String labelCode = l.getLabelCode();
                    return heroLabelInfos.stream().filter(x -> labelCode.equals(x.getParentLabelCode()));
                })
                .sorted(Comparator.comparing(HeroLabelInfo::getShowOrder))
                .map(x -> new IdAndNameVO(x.getLabelCode(), x.getLabelName()))
                .collect(Collectors.toList());
    }

    /**
     * 服务能力
     */
    public List<IdAndNameVO> getServiceList(List<HeroLabelInfo> heroLabelInfos) {
        return heroLabelInfos.stream().filter(x -> LABEL_SERVICE.equals(x.getParentLabelCode()))
                .sorted(Comparator.comparing(HeroLabelInfo::getShowOrder))
                .map(x -> new IdAndNameVO(x.getLabelCode(), x.getLabelName()))
                .collect(Collectors.toList());
    }

}
